<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->data['menu'] = 'admin';
		$this->data['form'] = 'custom/form_admin';
		$this->data['title'] = 'Administrator';
	}

	public function index()
	{
		if($this->session->userdata('admin')){
			$session_data = $this->session->userdata('admin');
			$this->data['username'] = $session_data['username'];
			$this->data['nama'] = $session_data['nama'];
			$this->data['type'] = $session_data['type'];
			redirect('home', 'refresh');
		}else{
			$data['url_login'] = 'verifylogin';
			$this->load->view('v_login', $data);
		}
	}
	
	function table(){
		$params = array('tablename' => 'admin', 'primary_key' => 'ad_uname');
		$this->load->library('grid', $params);
		$this->grid->add_subject('Administrator');
		$this->grid->add_field('ad_name', 'Nama Admin', '', 'show');
		$this->grid->add_field('ad_uname', 'ID', '', 'show');
		$this->grid->add_field('ad_pwd', 'Password', '', 'hidden');
		$this->grid->set_menu('admin');
		$this->grid->set_form('form_admin');
		$this->grid->set_portlet_style('portlet box purple');
		$this->grid->set_portlet_title('Administrator');
		$this->grid->show();
	}
	
	function init_form(){
		$this->data['ad_uname'] = '';
		$this->data['ad_name'] = '';
		$this->data['ad_pwd'] = '';
		$this->data['ad_pwd_conf'] = '';
		$this->data['tmp_pwd'] = '';
	}
	
	function add(){
		$this->init_form();
		$this->data['portlet_style'] = 'portlet box yellow';
		$this->data['portlet_title'] = 'Tambah Data Admin';
		$this->data['action'] = site_url('admin/action/insert');
		$this->data['portlet_type'] = 'Form';
		$this->data['id'] = '';
		$this->load();
	}
	
	function update($id){
		$this->load->model('m_admin');
		$row = (array)$this->m_admin->get_data_by_id($id);
		$this->data = array_merge($row, $this->data);
		$this->data['ad_pwd_conf'] = $this->data['ad_pwd'];
		$this->data['tmp_pwd'] = $this->data['ad_pwd'];
		$this->data['portlet_style'] = 'portlet box red';
		$this->data['portlet_title'] = 'Update Data Admin';
		$this->data['action'] = site_url('admin/action/update');
		$this->data['portlet_type'] = 'Form';
		$this->data['id'] = $id;
		$this->load();
		//print_r($this->data);
	}
	
	function delete($id){
		$this->load->model('m_admin');
		$res = $this->m_admin->delete(array('ad_uname' => $id));
		if($res!='1'){
			$this->data['error_message'] = 'Gagal';
		}
		redirect('admin/table');
	}
	
	function form_value(){
		$value['ad_uname'] = $this->input->post('ad_uname');
		$value['ad_name'] = $this->input->post('ad_name');
		$value['ad_pwd'] = $this->input->post('ad_pwd');
		$ad_pwd_conf = $this->input->post('ad_pwd_conf');
		
		if(strcmp($value['ad_pwd'], $ad_pwd_conf) != 0) $this->redirect('admin/add', '0', 'Password Tidak Sama');  
		
		$pwd = $this->input->post('ad_pwd');
		$tmp_pwd = $this->input->post('tmp_pwd');
		if($pwd == $tmp_pwd) $value['ad_pwd'] = $pwd;
		else $value['ad_pwd'] = md5($pwd);
		return $value;
	}

	function action(){
		$act = $this->uri->segment(3);
		$value = $this->form_value();
		$this->load->model('m_admin');
		$id = $this->input->post('ad_uname');
		switch ($act){
			case "insert":
				$value['ad_type'] = 2;
				$res = $this->m_admin->insert($value);
				$loc = 'admin/add';
				break;
			case "update":
				$res = $this->m_admin->update($value, array('ad_uname' => $id));
				$loc = 'admin/update/'.$id;
				break;
		}
		$this->redirect($loc, $res, "Gagal");
	}
	
	function redirect($loc, $res, $msg){
		if($res=='1'){
			redirect('admin/table');
		}else{
			$this->data['error_message'] = 'Gagal';
			redirect($loc);
		}
	}

	function load(){
		if($this->session->userdata('admin')){
			$session_data = $this->session->userdata('admin');
			$this->data['username'] = $session_data['username'];
			$this->data['nama'] = $session_data['nama'];
			$this->data['type'] = $session_data['type'];
			$this->load->view('main', $this->data);
		}else{
			redirect('admin', 'refresh');
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */